package com.kazusa.myjsp;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //获取客户端传递的参数
        String uname=request.getParameter("uname");
        String upwd=request.getParameter("upwd");

        //判断参数是否为空
        if(uname==null||"".equals(uname.trim())||upwd==null||"".equals(upwd.trim())){
            //提示用户信息
            request.setAttribute("msg","用户名或密码不能为空！");
            //请求转发跳转到login.jsp
            request.getRequestDispatcher("login.jsp").forward(request,response);
            return;
        }
        //判断用户名和密码是否正确
        if(!"admin".equals(uname)||!"123456".equals(upwd)){
            request.setAttribute("msg","用户名或密码错误！");
            request.getRequestDispatcher("login.jsp").forward(request,response);
            return;
        }

        //登录成功
        //设置session
        //跳转到首页
        request.getSession().setAttribute("uname",uname);
        response.sendRedirect("welcome.jsp");


    }
}
